package vista;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Toolkit;

import javax.swing.JLabel;

import controlador.Controlador;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.border.BevelBorder;

public class ItemPendiente extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	
	private Controlador controlador;
	private JTextField txtObs;
	private JTextField txtSolicitante;
	private JTextField txtCantidad;
	private JTextField txtEnStock;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxItemId;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxItem;
	private JLabel lblFoto;

	@SuppressWarnings("rawtypes")
	public ItemPendiente() {
		setResizable(false);
		setTitle("Solicitud");
		setIconImage(Toolkit.getDefaultToolkit().getImage(ItemPendiente.class.getResource("/modelo/imagenes/cute_pictures_2921cd4.jpg")));
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
			JLabel lblItem = new JLabel("Item");
			lblItem.setBounds(10, 47, 46, 14);
			contentPanel.add(lblItem);
		
			JLabel lblColor = new JLabel("Cantidad");
			lblColor.setBounds(10, 92, 81, 14);
			contentPanel.add(lblColor);
		
			JLabel lblTalla = new JLabel("Solicitante");
			lblTalla.setBounds(10, 117, 86, 14);
			contentPanel.add(lblTalla);
		
			JLabel lblDescrpicinDelItem = new JLabel("Nueva Solicitud");
			lblDescrpicinDelItem.setFont(new Font("Tahoma", Font.PLAIN, 20));
			lblDescrpicinDelItem.setBounds(10, 11, 244, 25);
			contentPanel.add(lblDescrpicinDelItem);
		
			JLabel label = new JLabel("Observaciones");
			label.setBounds(10, 142, 117, 14);
			contentPanel.add(label);
		
			txtObs = new JTextField();
			txtObs.setBounds(10, 167, 318, 20);
			contentPanel.add(txtObs);
			txtObs.setColumns(10);
		
			txtSolicitante = new JTextField();
			txtSolicitante.setBounds(101, 114, 86, 20);
			contentPanel.add(txtSolicitante);
			txtSolicitante.setColumns(10);
		
			txtCantidad = new JTextField();
			txtCantidad.setBounds(101, 89, 86, 20);
			contentPanel.add(txtCantidad);
			txtCantidad.setColumns(10);
		
		
			cbxItem = new JComboBox();
			cbxItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (!(cbxItem.getSelectedIndex()==0)) {
						cbxItemId.setSelectedIndex(cbxItem.getSelectedIndex());					
						controlador.getItemPendienteCtrl().buscarImagen(cbxItemId.getSelectedItem().toString(), lblFoto.getHeight(), lblFoto.getWidth());	
						txtEnStock.setText(controlador.getDespacharCtrl().buscarEnStock(cbxItemId.getSelectedItem().toString()));
					}else{
						borrarCampos();
					}
				}
			});
			cbxItem.setBounds(10, 61, 414, 20);
			contentPanel.add(cbxItem);
		
			lblFoto = new JLabel("");
			lblFoto.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			lblFoto.setBounds(338, 91, 86, 98);
			contentPanel.add(lblFoto);
		
			JLabel lblEnStock = new JLabel("En Stock");
			lblEnStock.setBounds(244, 92, 71, 14);
			contentPanel.add(lblEnStock);
		
			txtEnStock = new JTextField();
			txtEnStock.setEditable(false);
			txtEnStock.setBounds(229, 114, 86, 20);
			contentPanel.add(txtEnStock);
			txtEnStock.setColumns(10);
		
			cbxItemId = new JComboBox();
			cbxItemId.setBounds(101, 41, 76, 20);
//			contentPanel.add(cbxItemId);
		
		
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						
						controlador.getItemPendienteCtrl().setTxtCantidad(txtCantidad.getText());
						controlador.getItemPendienteCtrl().setTxtItem(cbxItemId.getSelectedItem().toString());
						controlador.getItemPendienteCtrl().setTxtObs(txtObs.getText());
						controlador.getItemPendienteCtrl().setTxtSolicitante(txtSolicitante.getText());			
						
						if (!txtCantidad.getText().isEmpty() && Integer.parseInt(txtCantidad.getText())>0) {
							if (Integer.parseInt(txtEnStock.getText())==0) {
								if (controlador.getItemPendienteCtrl().ingresarSolicitud()) {
									cerrarVentana();
								}
							}else {
								JOptionPane.showMessageDialog(null, "Item con Stock", "Advertencia", JOptionPane.WARNING_MESSAGE);
							}
						} else {
							JOptionPane.showMessageDialog(null, "La cantidad ingresada no es valida", "Advertencia", JOptionPane.WARNING_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cerrarVentana();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);			
	}
	
	
	public void setControlador(Controlador miControlador){
		this.controlador = miControlador;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setModeloComboboxItem(DefaultComboBoxModel x){
		cbxItem.setModel(x);
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setModeloCbxItemId(DefaultComboBoxModel x){
		cbxItemId.setModel(x);
	}
	public void cambiarImagen(Image icon){
		lblFoto.setIcon(new ImageIcon(icon));
	}
	
	public void borrarCampos(){
		lblFoto.setIcon(null);
		txtCantidad.setText("");
		txtSolicitante.setText("");
		txtObs.setText("");		
		txtEnStock.setText("");
		cbxItem.setSelectedIndex(0);
	}
	
	private void cerrarVentana() {
		controlador.ocultarVentanaItemPendiente();
		borrarCampos();
		controlador.getItemPendienteCtrl().actualizarTabla("Pendiente");
	}

}
